package com.mfp.java.testYou.core;

import com.mfp.java.testYou.db.model.TakenTest;
import com.mfp.java.testYou.db.model.User;
import com.mfp.java.testYou.db.model.UserRole;

public class UserSession {

	// private static final Logger log = Logger.getLogger(UserSession.class);

	public static final String ATTRIBUTE_NAME = "testYouSession";

	private static User JANE_DO = new User();
	static {
		JANE_DO.setLang("en");
		JANE_DO.setUserName("Jane Doe");
		JANE_DO.setRole(UserRole.ADMIN);
	}
	private static UserSession DEF_SESSION = new UserSession(JANE_DO) {
		// anonymous inner class
		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();
			sb.append("!NOT LOGGED IN!--  ");
			sb.append(super.toString());
			sb.append("  --!NOT LOGGED IN!");
			return sb.toString();
		}

		@Override
		public boolean isLoggedIn() {
			return false;
		}
	};

	private User userData = null;
	private String msg, err;
	private TakenTest takenTest;

	public UserSession(User user) {
		userData = user;
	}

	public boolean isLoggedIn() {
		return userData != null;
	}

	public final String getUserName() {
		if (userData == null) {
			return DEF_SESSION.getUserName();
		}
		return userData.getUserName();
	}

    public final int getId(){
        if (userData == null){
            return DEF_SESSION.getId();
        }
        return userData.getId();
    }

	public final boolean isAdmin() {
		if (userData == null) {
			return false;
		}
		return userData.getRole() == UserRole.ADMIN;
	}

	public final boolean isStudent() {
		if (userData == null) {
			return false;
		}
		return userData.getRole() == UserRole.USER;
	}

	@Override
	public String toString() {
		if (userData == null) {
			return DEF_SESSION.toString();
		}

		StringBuilder sb = new StringBuilder();
		sb.append(userData.getRole());
		sb.append(" - ");
		sb.append(userData.getLang());
		sb.append(" - ");
		sb.append(userData.getUserName());

		if (err != null && err.length() > 0) {
			sb.append(" - ");
			sb.append("err: " + err);
		}
		if (msg != null && msg.length() > 0) {
			sb.append(" - ");
			sb.append("msg: " + msg);
		}

		return sb.toString();
	}

	/**
	 * Will set a message to be shown to the user.
	 * 
	 * @param msg
	 *            Message to show
	 */
	public void setMessage(String msg) {
		this.msg = msg;
	}

	/**
	 * Will get the message set by previous servlet.
	 * 
	 * @param request
	 *            ServletRequest object.
	 * @return The set message. Will return null, if no message is set.
	 */
	public String popMessage() {
		String ret = msg;
		msg = null;
		return ret;
	}

	/**
	 * Will set an error message to shown to the user.
	 * 
	 * @param msg
	 *            Error to show
	 */
	public void setError(String msg) {
		this.err = msg;
	}

	/**
	 * Will get the error message set by previous servlet.
	 * 
	 * @return The set error message. Will return null, if no error is set.
	 */
	public String popError() {
		String ret = err;
		err = null;
		return ret;
	}

	
	
	public final TakenTest getTakenTest() {
		return takenTest;
	}

	public final void setTakenTest(TakenTest takenTest) {
		this.takenTest = takenTest;
	}
}
